package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.Message;
import cn.zhf.shop.mapper.ConsoleUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import cn.zhf.shop.entity.AgentApplication;
import cn.zhf.shop.mapper.AgentApplicationMapper;
import cn.zhf.shop.service.AgentApplicationService;

@Service
public class AgentApplicationServiceImpl implements AgentApplicationService {

    @Resource
    private AgentApplicationMapper agentApplicationMapper;
    @Resource
    private ConsoleUserMapper consoleUserMapper;

    @Override
    public Message deleteByPrimaryKey(Integer agentApplicationId) {
        return agentApplicationMapper.deleteByPrimaryKey(agentApplicationId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message insert(AgentApplication record) {
        record.setAgentApplicationPostTime(new Date());
        return agentApplicationMapper.insert(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message insertSelective(AgentApplication record) {
        record.setAgentApplicationPostTime(new Date());
        return agentApplicationMapper.insertSelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<AgentApplication> selectByPrimaryKey(Integer agentApplicationId) {
        return Message.success(null).add(agentApplicationMapper.selectByPrimaryKey(agentApplicationId));
    }

    @Override
    public Message updateByPrimaryKeySelective(AgentApplication record) {
        return agentApplicationMapper.updateByPrimaryKeySelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(AgentApplication record) {
        return agentApplicationMapper.updateByPrimaryKey(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<List<AgentApplication>> selectAll() {
        return Message.success(null).add(agentApplicationMapper.selectAll());
    }

    @Override
    public Message updateAgentApplicationStatusByAgentApplicationId(Integer updatedAgentApplicationStatus, Integer agentApplicationId) {
        if (updatedAgentApplicationStatus==1){
            AgentApplication agentApplication = agentApplicationMapper.selectByPrimaryKey(agentApplicationId);
            consoleUserMapper.updateLevelByUserId(agentApplication.getAgentApplicationLevel(),agentApplication.getAgentApplicationUserId());
        }
        return agentApplicationMapper.updateAgentApplicationStatusByAgentApplicationId(updatedAgentApplicationStatus, agentApplicationId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<List<AgentApplication>> selectByAgentApplicationUserId(Integer agentApplicationUserId) {
        return Message.success(null).add(agentApplicationMapper.selectByAgentApplicationUserId(agentApplicationUserId));
    }

}

